package top.csy8888.work2.web.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.csy8888.work2.common.ResultVO;
import top.csy8888.work2.entity.Goods;
import top.csy8888.work2.service.GoodsService;

import java.util.List;

/**
 * @author CSY
 * @date 2025 - 09 - 17
 */
@RestController
@RequestMapping("/api/v1")

@Tag(name = "商品服务接口",description = "提供关于商品的API操作接口")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/goods")
    @Operation(
            description = "分页获取商品信息列表",
            method = "GET",
            summary = "分页商品信息"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "响应成功"),
            @ApiResponse(responseCode = "500",description = "响应失败，服务器错误！！")
    })
    @Parameters({
            @Parameter(name = "pageNum",description = "当前数据所处的页数",in = ParameterIn.HEADER),
            @Parameter(name = "pageSize",description = "每页显示多少条数据",in = ParameterIn.HEADER)
    })
    public ResultVO<List<Goods>> listGoods(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize) {
        List<Goods> goods = goodsService.listGoods(pageNum, pageSize);
        return ResultVO.success(goods);
    }

    @GetMapping("/user/{id}")
    @Operation(description = "根据商品编号查询商品信息",method = "GET",summary = "查询商品")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "响应成功"),
            @ApiResponse(responseCode = "500",description = "响应失败，服务器错误！")
    })
    @Parameters({
            @Parameter(name = "id",description = "商品编号",in = ParameterIn.PATH)
    })
    public ResultVO<Goods> getGoodsById(@PathVariable("id") Integer id) {
        Goods g = goodsService.getGoodsById(id);
        return ResultVO.success(g);
    }

    @PostMapping("/goods")
    @Operation(description = "根据传入的商品对象添加商品",method = "POST",summary = "添加商品")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "响应成功"),
            @ApiResponse(responseCode = "500",description = "响应失败，服务器错误！")
    })
    @Parameters({
            @Parameter(name = "goods",description = "要添加的商品对象",in = ParameterIn.DEFAULT)
    })
    public ResultVO<String> addGoods(@RequestBody Goods goods) {
        goodsService.addGoods(goods);
        return ResultVO.success();
    }

    @PutMapping("/goods")
    @Operation(description = "根据传入的商品对象修改商品",method = "PUT",summary = "修改商品")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "响应成功"),
            @ApiResponse(responseCode = "500",description = "响应失败，服务器错误！")
    })
    @Parameters({
            @Parameter(name = "goods",description = "修改后的商品对象",in = ParameterIn.DEFAULT)
    })
    public ResultVO<String> updateGoods(@RequestBody Goods goods) {
        goodsService.updateGoods(goods);
        return ResultVO.success();
    }

    @DeleteMapping("/goods/{id}")
    @Operation(description = "根据传入的商品编号删除商品",method = "DELETE",summary = "删除商品")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "响应成功"),
            @ApiResponse(responseCode = "500",description = "响应失败，服务器错误！")
    })
    @Parameters({
            @Parameter(name = "id",description = "要删除的商品编号",in = ParameterIn.PATH)
    })
    public ResultVO<String> deleteGoods(@PathVariable("id") Integer id) {
        goodsService.deleteGoods(id);
        return ResultVO.success();
    }
}
